import java.util.Stack;

public class quickSort {

    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }
    private static void quick(int[] array,int start,int end) {
        if(start >= end) {
            return;
        }
        //递归到一定的区间，使用插入排序(优化)
        if((end-start+1) < 5) {
            insertSort(array,start,end);
            return;
        }

        //三数取中法（优化）
        int index = findMid(array,start,end);
        swap(array,index,start);
        int pivot = partition2(array,start,end);
        //递归左边
        quick(array,start,pivot-1);
        //递归右边
        quick(array,pivot+1,end);

    }

    /**
     * 区间插入排序
     */
    private static void insertSort(int[] array,int start,int end) {
        for (int i = start+1; i <= end; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= start ; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }


    /*
        找中间大的元素下标
     */
    private static int findMid(int[] array,int left,int right) {
        int mid = left+( (right - left) >> 1 );
        if(array[left] > array[right]) {
           if(array[left] < array[mid]) {
               return left;
           }else if(array[right] > array[mid]) {
               return right;
           }else  {
               return mid;
           }
        }else {
            if(array[mid] > array[right]) {
                return right;
            }else if(array[left] > array[mid]) {
                return left;
            }else {
                return mid;
            }
        }
    }

    /**
     *  交换元素并且返回left和right相遇的位置下标
     * @return下标
     */
    //第一种：Hoare版
    private static int partition1(int[] array,int left,int right) {
        int tmp = array[left];//基准
        int l = left;
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,l,right);

        return right;
    }
    //第二种：挖坑法
    private static int partition2(int[] array,int left,int right) {
        int tmp = array[left];//一个坑

        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[right] = tmp;
        return right;

    }
    private static void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    /**
     *  快速排序非递归
     */
    public static void quickSortNor(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length - 1;
        //1.找第一次基准
        int pivot = partition2(array,left,right);
        //2.将基准左右两边存入栈中
        if(pivot-1>left) {
            stack.push(left);
            stack.push(pivot-1);
        }
        if(pivot+1<right) {
            stack.push(pivot+1);
            stack.push(right);
        }
        //3.栈不为空出栈
        while (!stack.empty()) {
            right = stack.pop();
            left = stack.pop();
            pivot = partition2(array,left,right);
            //2.将基准左右两边存入栈中
            if(pivot-1>left) {
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot+1<right) {
                stack.push(pivot+1);
                stack.push(right);
            }
        }


    }


}
